/**
 *
 */
package org.starsource.forum.server.transaction.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Set;

import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.client.exception.MessageNotFoundException;
import org.starsource.forum.client.exception.PermissionDeniedException;
import org.starsource.forum.client.exception.ResourceNotFoundException;
import org.starsource.forum.client.exception.SessionNotFoundException;
import org.starsource.forum.server.dao.MessageDao;
import org.starsource.forum.server.dao.ResourceDao;
import org.starsource.forum.server.domain.Category;
import org.starsource.forum.server.domain.Message;
import org.starsource.forum.server.domain.Resource;
import org.starsource.forum.server.domain.Session;
import org.starsource.forum.server.domain.User;
import org.starsource.forum.server.transaction.MessengerTransaction;
import org.starsource.forum.server.transaction.SessionTransaction;

/**
 * @author james
 *
 */
public class ForumTransactionImpl implements MessengerTransaction {
//	private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
//	.getLogger(ForumTransactionImpl.class.getName());

	private SessionTransaction sessionTransaction;
	public void setSessionTransaction(SessionTransaction sessionTransaction) {
		this.sessionTransaction = sessionTransaction;
	}

	private MessageDao messageDao;
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	private ResourceDao resourceDao;
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	@Override
	public Session createSession(User requester, String title, String content, Category category) throws DatabaseException {
		Session session = sessionTransaction.createSession(requester, title, category);
		Timestamp timestamp = new Timestamp(new Date().getTime()/1000*1000);
		Message message = new Message(requester, content, session, timestamp, timestamp);
		session.getMessages().add(message);
		messageDao.save(message);
		return session;
	}
	@Override
	public Message followSession(User requester, String content, Session session) throws DatabaseException {
//		logger.debug("Number of attached resources: " + resourceUrls.size());
		Timestamp timestamp = new Timestamp(new Date().getTime()/1000*1000);
		Message message = new Message(requester, content, session, timestamp, timestamp);
		session.getMessages().add(message);
		messageDao.save(message);
//		for(int j=0; j<resourceUrls.size(); j++) {
//			logger.debug("ADD resource: " + resourceUrls.get(j));
//			Resource resource = new Resource(message, resourceUrls.get(j));
//			message.getResources().add(resource);
//			resourceDao.save(resource);
//		}
		return message;
	}

	public Session	getSession(User requester, int id) throws SessionNotFoundException, DatabaseException {
		return sessionTransaction.getSession(requester, id);
	}

	@Override
	public Set<Session> getSessionSet(User requester) throws DatabaseException {
		return sessionTransaction.getSessionSet(requester);
	}

	@Override
	public Set<Session> getSessionSet(User requester, Category category) throws DatabaseException {
		return sessionTransaction.getSessionSet(requester, category);
	}

	public void		updateSession(User requester, Session session) throws DatabaseException {
		sessionTransaction.updateSession(requester, session);
	}

	public void		updateMessage(User requester, Message message) throws DatabaseException {
		messageDao.update(message);
	}

	public Message	getMessage(User requester, int id) throws MessageNotFoundException, DatabaseException {
		return messageDao.getMessage(id);
	}

	@Override
	public Resource addResource(User requester, String url, String thumbUrl, int width, int height, Message message) throws DatabaseException {
		Resource resource = new Resource(message, url, thumbUrl, width, height);
		message.getResources().add(resource);
		resourceDao.save(resource);
		return resource;
	}

	@Override
	public void removeResource(User requester, Resource resource) throws ResourceNotFoundException, DatabaseException {
		resourceDao.delete(resource);
	}

	@Override
	public void removeSession(User requester, Session session) throws PermissionDeniedException, DatabaseException {
		sessionTransaction.removeSession(requester, session);
	}

	@Override
	public void removeMessage(User requester, Message message) throws MessageNotFoundException, DatabaseException {
		messageDao.delete(message);
	}

	@Override
	public Resource getResource(User requester, int id)
			throws ResourceNotFoundException, DatabaseException {
		return resourceDao.getResource(id);
	}

	@Override
	public Set<Session> getSessionSet(User requester, Set<Category> categories)
			throws DatabaseException {
		return sessionTransaction.getSessionSet(requester, categories);
	}

	@Override
	public Set<Session> getSessionSetByKeywords(User requester, String[] keywords) throws DatabaseException {
		return sessionTransaction.getSessionSetByKeywords(requester, keywords);
	}

}
